
//初始化函数
var exitSn;
$(function(){
     getData();
     exitSn=getParam('ex');
     showErrMsg();
});
//参数截取 getParam
/*	setDef--车牌设置为默认*/
const getParam=(param)=>{
        var reg = new RegExp('(^|&)' + param + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
        return unescape(r[2]);
        }
        return null;
};
const getData=()=>{
        $.ajax({
            url:'/member/carplate/list',
            type:'post',
            success:function(result){
               handlRender($('#plateTpl'),result,$('.plate-list')); 
            }
        })
};
const handlRender=(tpl,tpl_data,appendBox)=>{
        var tpl=tpl.html(),
            template = Handlebars.compile(tpl),
            tplHtml = template(tpl_data);
            appendBox.html(tplHtml)
};
const setDef=(carPlateId)=>{
    $.ajax({
        url:' /member/carplate/setdef',
        type:'post', 
        data:{'carPlateId':carPlateId},
        success:function(res){
                if(res.status==0){
						layer.msg("设置成功！", {icon: 1,shade: [0.8, '#393D49'],time: 1000});
						getData();                      	                                      
                }else{
                    layer.msg("设置失败！", {icon: 5,shade: [0.8, '#393D49'],time: 1000});
                        
                }
        }
    })	
};
   const removePlate=(carPlateId)=>{
		layer.open({
			  content: '您确定删除这条信息吗？'
			  ,btn: ['确定', '取消']
			  ,yes: function(index, layero){
		            $.ajax({
		                url:'/member/carplate/delete',
		                type:'post', 
		                data:{'carPlateId':carPlateId},
		                success:function(res){
		                        if(res.status==0){
		                        	layer.msg('删除成功！', {icon: 1,shade: [0.8, '#393D49'],time: 1000});
		  							getData();                      	                                      
		                        }else{
		                        	layer.msg('删除失败！', {icon: 5,shade: [0.8, '#393D49'],time: 1000});		                                
		                        }
		                }

		            })
			    
			  }
			  ,btn2: function(index, layero){
			    //按钮【按钮二】的回调	  
	/*		     layer.alert('您取消了操作！', {icon: 2});  */
			    //return false 开启该代码可禁止点击该按钮关闭
			  }
			  ,cancel: function(){ 
			    //右上角关闭回调	    
			    //return false 开启该代码可禁止点击该按钮关闭
			  }
			});	          
        };
function showErrMsg(){
	var error_msg=$('#error_msg').val();
    if(error_msg != ''){
    	layer.msg(error_msg, {icon: 5,shade: [0.8, '#393D49'],time: 2000});
    }
}
// 添加车牌       
$('.add-plate').click(function(){
	showErrMsg();
	window.location.href='/member/carplate/bind?ex='+exitSn;
});       
///删除车牌动作    
    $('.plate-list').on('click','.delete',function(){
        let id=$(this).parent(".handle-bt").attr('data-id');
        removePlate(id);       
    });
///车牌默认动作   
    $('.plate-list').on('click','.primary',function(){
    	let id=$(this).parent(".handle-bt").attr('data-id');
    	let isDef=$(this).hasClass('default');
    	if(!isDef){
    		setDef(id); 	
    	};      
    });        
    
    